﻿#ifndef DIALOG_H
#define DIALOG_H

#include "m_fhs.h"
#include <element/baserestreceiver.h>
#include <support/flashdb.h>
#include <QSystemTrayIcon>
#include <QStandardItemModel>
#include <QStandardItem>
#include "itemview.h"

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT

public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();

    void startReceiver();
    void stopReceiver();
    void onReceivedObject(const QJsonObject &object);
    void onError();

    void processCategory(const QJsonArray &array);
    void processDetail(const QJsonArray &array);

    void createMenu();
    void createTray();
    void updateView();
    void updateMenu();
    void makeupFlashdb();
    void syncDataScource();

    void removePicture();
    void collapseAll();
    void showStatus(const QString &message, int interval=4000);
    void showItemView(const QString &label, const QString &detail);

signals:
    void get(const QString &url);
    void post(const QString &url, const QByteArray &data);

protected:
    void timerEvent(QTimerEvent *event);
    void contextMenuEvent(QContextMenuEvent *);

protected slots:
    void trayActivated(QSystemTrayIcon::ActivationReason);

private slots:
    void on_treeView_clicked(const QModelIndex &index);
    void on_treeView_doubleClicked(const QModelIndex &index);
    void on_input_returnPressed();

private:
    Ui::Dialog *ui;

    QString baseUrl;
    int form;
    BaseRestReceiver *receiver;
    QStandardItemModel *itemModel;
    QThread recvThread;
    FlashDB db;
    QString selectCategory;
    QString selectLabel;
    QString selectDetail;

    QSystemTrayIcon *tray;
    QMenu *tray_Menu;
    QMenu *menu;

    ItemView *view;
    int cleanTimer;
    bool isStatusIdle;
    QMap<QString,QString> resultCache;
};

#endif // DIALOG_H
